<?php
include_once("class\Model\Model.userMemory.php");
include_once("inc\usedb_inc.php");
class DAL_userMemory
{
	public static function insert($userMemory)
	{
		$p=array(":userid"=>$userMemory->userid,":username"=>$userMemory->username,":wordid"=>$userMemory->wordid);
		//$sql="insert into userMemory (userid,username,wordid,memory) values(:userid,:username,:wordid,:memory)";
		if($userMemory->memory!=null &&$userMemory->memory!="")
		{
			$p[":memory"]=$userMemory->memory;
		}
		if($userMemory->testCount!=null &&$userMemory->testCount!="")
		{
			$p[":testCount"]=$userMemory->testCount;
		}
		if($userMemory->testerrorCount!=null &&$userMemory->testerrorCount!="")
		{
			$p[":testerrorCount"]=$userMemory->testerrorCount;
		}
		if($userMemory->errorRate!=null &&$userMemory->errorRate!="")
		{
			$p[":errorRate"]=$userMemory->errorRate;
		}
		
		$cols="";
		$val="";
		foreach ($p as $key => $value)
		{
			$cols.=trim($key, ":").",";
			$val.=$key.",";
		}
		$cols=trim($cols, ",");
		$val=trim($val, ",");
		
		$sql="insert into userMemory ($cols) values($val)";
		
		//echo $sql;
		
		$s=new SqliteHelper();
		$s->ExecNoTans($sql,$p);
	}
	
	public static function insertOrUpdate($userMemory)
	{
		//print_r($userMemory);
		$result=self::select($userMemory);
		//print_r($result);
		if(count($result)>0)
		{
			self::update($userMemory);
		}
		else
		{
			self::insert($userMemory);
		}
	}
	
	public static function update($userMemory)
	{
		$p=array(":userid"=>$userMemory->userid,":username"=>$userMemory->username,":wordid"=>$userMemory->wordid);
		if($userMemory->memory!=null &&$userMemory->memory!="")
		{
			$p[":memory"]=$userMemory->memory;
		}
		if($userMemory->testCount!=null &&$userMemory->testCount!="")
		{
			$p[":testCount"]=$userMemory->testCount;
		}
		if($userMemory->testerrorCount!=null &&$userMemory->testerrorCount!="")
		{
			$p[":testerrorCount"]=$userMemory->testerrorCount;
		}
		if($userMemory->errorRate!=null &&$userMemory->errorRate!="")
		{
			$p[":errorRate"]=$userMemory->errorRate;
		}
		
		//print_r($p);
		
		//$cols="";
		$val="";
		foreach ($p as $key => $value)
		{
			$val.=trim($key, ":")."=$key,";
		}

		$val=trim($val, ",");
		
		$sql="update userMemory set $val where userid=:userid and username=:username and wordid=:wordid ";
		//print_r($p);
		//echo 	"<br>$sql";	
		$s=new SqliteHelper();
		$s->ExecNoTans($sql,$p);
	}
	
	public static function select($userMemory)
	{
		$p=array();
		$sql="select * from userMemory where 1=1 ";
		//print_r($userMemory);
		if(!empty($userMemory->userid))
		{
			$sql.=" and userid=:userid ";
			$p[":userid"]=$userMemory->userid;
		}
		if(!empty($userMemory->username))
		{
			$sql.=" and username=:username ";
			$p[":username"]=$userMemory->username;
		}
		if(!empty($userMemory->wordid))
		{
			$sql.=" and wordid=:wordid ";
			$p[":wordid"]=$userMemory->wordid;
		}

		$s=new SqliteHelper();
		return $s->Query($sql,$p);
	}
}
